package interfaz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JWindow;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.border.SoftBevelBorder;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class Popup extends JFrame implements KeyListener,MouseListener{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JWindow jWindow = null;  //  @jve:decl-index=0:visual-constraint="697,125"
	private JScrollPane jScrollPane = null;
	private JTextPane jTextPane = null;
	/**
	 * This is the default constructor
	 */
	public Popup() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(621, 521);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("Ventana");
		String texto="Da xoDA fsdfsdf asdfsadf sdfsaf a sdfsd sdfsd fsdf sdfds fsdf sdf sdf sdf sdf das das as asd asd as sdfasfas asfasdfasf sadfasdfsa sadfsfsaf asfsadfasf asfasdfasd fasdfasdfasf asdfsafsadf sadfsadfasdf asdfsadfsadf sadfsadfsad";
		this.colorearTexto(texto);
	//	this.getJTextPane().setText(texto);
		
		this.getJWindow().setSize(350,5);
		
		
		//11 ancho para la segunda,7 para la tercera y de ahi para adelante, 20 para la primera letra
		//22 alto para el primer renglon,17 para el segundo
		
		this.getJWindow().setLocation(300,200);
		this.getJWindow().setVisible(true);
		this.redimensionar();
		this.getJTextPane().setEditable(false);
		   
	}
	
	private void redimensionar()
	{
		Rectangle caretCoords;
		int cantidadRenglones=0;
		try {
			caretCoords = this.getJTextPane().modelToView(this.getJTextPane().getCaret().getDot());
			int  x = (int) caretCoords.getY();
			int lineHeight = this.getJTextPane().getFontMetrics(this.getJTextPane().getFont()).getHeight();
			cantidadRenglones=(x / lineHeight) + 1;
		} catch (BadLocationException e) {
		}
		int alto=22+((cantidadRenglones-1)*17);
		this.getJWindow().setSize(350,alto);
	}


	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jWindow	
	 * 	
	 * @return javax.swing.JWindow	
	 */
	private JWindow getJWindow() {
		if (jWindow == null) {
			jWindow = new JWindow(this);
			jWindow.setSize(new Dimension(358, 462));
			jWindow.setContentPane(getJScrollPane());
		}
		return jWindow;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
			jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
			jScrollPane.setViewportView(getJTextPane());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextPane	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getJTextPane() {
		if (jTextPane == null) {
			jTextPane = new JTextPane();
		}
		return jTextPane;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode()==27)
		{
			this.getJWindow().dispose();
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}
	
	private void colorearTexto(String texto)
	{
//		creamos la clase StyleContext en la que nos apoyaremos
		StyleContext sc = new StyleContext();

//		primer tipo de estilo... color rojo
		Style estiloAzul = sc.addStyle(null, null);
		StyleConstants.setForeground(estiloAzul, Color.BLUE);
		StyleConstants.setUnderline(estiloAzul, true);
//		tambien existen los método setBackground(), setFont()

//		para el negro
		Style estiloNegro = sc.addStyle(null, null);
		StyleConstants.setForeground(estiloNegro, Color.black);
		
//		creamos el StyledDocument pasandole la clase
//		StyleContext que hemos creado antes...
		DefaultStyledDocument doc = new DefaultStyledDocument(sc);
		
		JTextPane texto1 = new JTextPane(doc); 
		texto1.setFont(new Font("Dialog", Font.BOLD, 12));
		texto1.setToolTipText("precione ESC para cerrar");
		texto1.addKeyListener(this);
		texto1.addMouseListener(this);
		this.jTextPane=texto1;
		
		try {
			doc.insertString(doc.getLength(), texto.substring(0, 3), estiloNegro);
			doc.insertString(doc.getLength(), texto.substring(3, 7), estiloAzul);
			doc.insertString(doc.getLength(), texto.substring(7, texto.length()), estiloNegro);
		} catch (BadLocationException e) {
			e.printStackTrace();
		} 
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getComponent()==this.getJTextPane())
		{
			if(this.getJTextPane().getCaretPosition()>3 && this.getJTextPane().getCaretPosition()<7)
			{
				System.out.println("Hipervinculo= xoDA");
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
